package com.umeox.watch.moto.launcher.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.umeox.moto.watch.themes.widget.ImageView;
import com.umeox.moto.watch.themes.widget.RoundedImageView;
import com.umeox.moto.watch.themes.widget.TextView;
import com.umeox.watch.moto.apiclient.model.FriendStepDTO;
import com.umeox.watch.moto.launcher.R;
import com.umeox.watch.moto.launcher.base.GlideApp;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class ChallengeStepRankingAdapter extends RecyclerView.Adapter<ChallengeStepRankingAdapter.Holder> {


    private final Context mContext;

    //判断是否全为0
    boolean isAllZero = true;

    private final ArrayList<FriendStepDTO> friendStepDTOArrayList = new ArrayList<>();

    public void setFriendStepOTDList(List<FriendStepDTO> list) {
        if (list.isEmpty()) {
            return;
        }
        friendStepDTOArrayList.clear();
        friendStepDTOArrayList.addAll(list);
        for (FriendStepDTO friendStepDTO : friendStepDTOArrayList) {
            if (friendStepDTO.amount != 0) {
                isAllZero = false;
                break;
            }
        }
        notifyDataSetChanged();
    }

    public ChallengeStepRankingAdapter(Context context) {
        mContext = context;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(mContext).inflate(R.layout.item_challenge_step_ranking_layout, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        holder.bind(position, friendStepDTOArrayList.get(position));
    }

    @Override
    public int getItemCount() {
        return friendStepDTOArrayList.size();
    }

    protected class Holder extends RecyclerView.ViewHolder {
        private final RoundedImageView ivAvatar;
        private final TextView tvName;
        private final TextView tvNumber;
        private final TextView tvRanking;
        private final ImageView ivRanking;

        public Holder(@NonNull View itemView) {
            super(itemView);
            ivAvatar = itemView.findViewById(R.id.iv_avatar);
            tvName = itemView.findViewById(R.id.tv_name);
            tvNumber = itemView.findViewById(R.id.tv_number);
            tvRanking = itemView.findViewById(R.id.tv_ranking);
            ivRanking = itemView.findViewById(R.id.iv_ranking);
        }


        @SuppressLint("SetTextI18n")
        public void bind(int position, FriendStepDTO friendStepDTO) {
            if (isAllZero) {
                tvRanking.setVisibility(View.INVISIBLE);
                ivRanking.setVisibility(View.INVISIBLE);
            } else {
                if (position < 3) {
                    tvRanking.setVisibility(View.INVISIBLE);
                    ivRanking.setVisibility(View.VISIBLE);
                    ivRanking.setImageLevel(position);
                } else {
                    tvRanking.setVisibility(View.VISIBLE);
                    ivRanking.setVisibility(View.INVISIBLE);
                    tvRanking.setText(String.format(Locale.ENGLISH, "%d", position + 1));
                }
            }

            tvName.setText(friendStepDTO.name);
            tvNumber.setText("" + friendStepDTO.amount);
            showAvatar(ivAvatar, friendStepDTO.avatar);
        }

        private void showAvatar(ImageView ivAvatar, String photoUrl) {
            GlideApp.with(mContext)
                    .load(photoUrl)
                    .placeholder(R.drawable.ic_default_avatar_48)
                    .error(R.drawable.ic_default_avatar_48)
                    .apply(RequestOptions.overrideOf(48).centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL))
                    .into(ivAvatar);
        }
    }

}
